﻿using UnityEngine;
using UnityEditor;
using BehaviorDesigner.Editor;
using BehaviorDesigner.Runtime;
using System.IO;

namespace nEditor
{
    public partial class ZoneMenu
    {

        private static GUIContent
            LoadBTButtonContent = new GUIContent("转换行为树", "load bt file");

        void ZoneFrameBt2CSV()
        {
            if (GUILayout.Button(LoadBTButtonContent))
            {
                LoadBufferBT(ZoneConfig.BuffBTAssetsPath + "buffer.asset");
            }

            AssetDatabase.Refresh();
        }

        void LoadBufferBT(string o)
        {
            ExternalBehaviorTree bt = (ExternalBehaviorTree)AssetDatabase.LoadAssetAtPath(o, typeof(ExternalBehaviorTree));

            string sV = string.Empty;
            string sType = string.Empty;
            string sComment = string.Empty;

            for (int i = 0; i < bt.BehaviorSource.GetAllVariables().Count; ++i)
            {
                SharedVariable sv = bt.BehaviorSource.GetAllVariables()[i];
                sV += sv.Name;
                sV += ",";

                sType += sv.GetType().Name;
                sType += ",";
            }

            string path = ZoneConfig.BuffBTAssetsPath + bt.name + "_bt.csv";

            var streamWriter = new StreamWriter(path);
            streamWriter.Write(sV);
            streamWriter.Write("\n");
            streamWriter.Write(sType);
            streamWriter.Close();
        }
    }
}
